package com.wing.bill.service;

import com.wing.bill.controller.admin.ReportUtil;
import com.wing.bill.dao.CabinetFormDao;
import com.wing.bill.dao.CheckReportDao;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.LocalDateTimeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CheckReportServiceImpl implements CheckReportService {
    @Autowired
    private CheckReportDao checkReportDao;
    @Autowired
    private CabinetFormDao cabinetFormDao;

    @Override
    public JsonResult list(String param, String code) {
        HashMap hm = new HashMap();
        hm.put("title", "盘验统计(按天)");
        hm.put("data", checkReportDao.list(ReportUtil.hundredDays().get(0), ReportUtil.hundredDays().get(1), code, param));
        return JsonResult.success(hm);
    }

    @Override
    public JsonResult topCell(String type, String startTime, String endTime, Integer click, String mainCabCode, String code) {
        if (StringUtils.isNotBlank(type) && click == 0) {//顶部数据  固定时间
            HashMap map = new HashMap() {{
                put("title", "待盘验总数量(支)");
                put("sign", "1");
            }};
            HashMap map1 = new HashMap() {{
                put("title", "已盘验总数量(支)");
                put("sign", "2");
            }};
            HashMap map2 = new HashMap() {{
                put("title", "已盘验报修数量(支)");
                put("sign", "3");
            }};
            HashMap map3 = new HashMap() {{
                put("title", "已盘验报废数量(支)");
                put("sign", "4");
            }};
            HashMap map4 = new HashMap() {{
                put("title", "异常刀具数量(支)");
                put("sign", "5");
            }};
            HashMap map5 = new HashMap() {{
                put("title", "盘验缺失刀具数量(支)");
                put("sign", "6");
            }};
            map.putAll(cabinetFormDao.checkTopQueryNotCheckNum(code, mainCabCode));//待盘验产品数
            if (type.equals("0")) {
                map1.putAll(checkReportDao.checkTop(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));//已盘验总数量
                map2.putAll(cabinetFormDao.checkTopQueryRepairOrderNum(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));//已盘验修磨产品数
                map3.putAll(cabinetFormDao.checkTopQueryRecycleOrderNum(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));//已盘验报废产品数
                map4.putAll(cabinetFormDao.checkTopQueryUnusualNum(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));//盘验异常产品数
                map5.putAll(cabinetFormDao.checkTopQueryDefectNum(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));//盘验缺失数
            } else if (type.equals("1")) {
                map1.putAll(checkReportDao.checkTop(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));//已盘验总数量
                map2.putAll(cabinetFormDao.checkTopQueryRepairOrderNum(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));//已盘验修磨产品数
                map3.putAll(cabinetFormDao.checkTopQueryRecycleOrderNum(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));//已盘验报废产品数
                map4.putAll(cabinetFormDao.checkTopQueryUnusualNum(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));//盘验异常产品数
                map5.putAll(cabinetFormDao.checkTopQueryDefectNum(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));//盘验缺失数
            } else {
                map1.putAll(checkReportDao.checkTop(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));//已盘验总数量
                map2.putAll(cabinetFormDao.checkTopQueryRepairOrderNum(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));//已盘验修磨产品数
                map3.putAll(cabinetFormDao.checkTopQueryRecycleOrderNum(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));//已盘验报废产品数
                map4.putAll(cabinetFormDao.checkTopQueryUnusualNum(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));//盘验异常产品数
                map5.putAll(cabinetFormDao.checkTopQueryDefectNum(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));//盘验缺失数
            }
            List<HashMap> list = new ArrayList();
            list.add(map);
            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
            return JsonResult.success("查询成功", list);
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 0) {//顶部数据  指定时间
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            HashMap map = cabinetFormDao.checkTopQueryNotCheckNum(code, mainCabCode);//待盘验产品数
            map.put("title", "待盘验总数量(支)");
            map.put("sign", "1");
            HashMap map1 = checkReportDao.checkTop(start, end, code, mainCabCode);//已盘验总数量
            map1.put("title", "已盘验总数量(支)");
            map1.put("sign", "2");
            HashMap map2 = cabinetFormDao.checkTopQueryRepairOrderNum(start, end, code, mainCabCode);//盘验修磨产品数
            map2.put("title", "已盘验报修数量(支)");
            map2.put("sign", "3");
            HashMap map3 = cabinetFormDao.checkTopQueryRecycleOrderNum(start, end, code, mainCabCode);//盘验报废产品数
            map3.put("title", "已盘验报废数量(支)");
            map3.put("sign", "4");
            HashMap map4 = cabinetFormDao.checkTopQueryUnusualNum(start, end, code, mainCabCode);//盘验异常数
            map4.put("title", "异常刀具数量(支)");
            map4.put("sign", "5");
            HashMap map5 = cabinetFormDao.checkTopQueryDefectNum(start, end, code, mainCabCode);//盘验缺失数
            map5.put("title", "盘验缺失刀具数量(支)");
            map5.put("sign", "6");
            List<HashMap> list = new ArrayList();
            list.add(map);
            list.add(map1);
            list.add(map2);
            list.add(map3);
            list.add(map4);
            list.add(map5);
            return JsonResult.success("查询成功", list);
        }
        if (StringUtils.isNotBlank(type) && click == 1) {//待盘验产品数 固定时间
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryNotCheckNumClick(code, mainCabCode));
        }
        if (StringUtils.isNotBlank(type) && click == 3) {//盘验修磨产品数 固定时间
            if (type.equals("0")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRepairOrderNumClick(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));
            } else if (type.equals("1")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRepairOrderNumClick(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));
            } else {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRepairOrderNumClick(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));
            }
        }
        if (StringUtils.isNotBlank(type) && click == 4) {//盘验报废产品数 固定时间
            if (type.equals("0")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRecycleOrderNumClick(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));
            } else if (type.equals("1")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRecycleOrderNumClick(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));
            } else {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRecycleOrderNumClick(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));
            }
        }
        if (StringUtils.isNotBlank(type) && click == 2) {//已盘验总数量 固定时间
            if (type.equals("0")) {
                return JsonResult.success("查询成功", checkReportDao.checkTopClick(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));
            } else if (type.equals("1")) {
                return JsonResult.success("查询成功", checkReportDao.checkTopClick(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));
            } else {
                return JsonResult.success("查询成功", checkReportDao.checkTopClick(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));
            }
        }
        if (StringUtils.isNotBlank(type) && click == 5) {//盘验异常数 固定时间
            if (type.equals("0")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryUnusualNumClick(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));
            } else if (type.equals("1")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryUnusualNumClick(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));
            } else {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryUnusualNumClick(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));
            }
        }
        if (StringUtils.isNotBlank(type) && click == 6) {//盘验缺失数
            if (type.equals("0")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryDefectNumClick(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), code, mainCabCode));
            } else if (type.equals("1")) {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryDefectNumClick(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), code, mainCabCode));
            } else {
                return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryDefectNumClick(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), code, mainCabCode));
            }
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 1) {//待盘验任务数 指定时间
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryNotCheckNumClick(code, mainCabCode));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 3) {//盘验修磨产品数 指定时间
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRepairOrderNumClick(start, end, code, mainCabCode));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 4) {//盘验报废产品数 指定时间
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryRecycleOrderNumClick(start, end, code, mainCabCode));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 2) {//已盘验总数量 指定时间
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功", checkReportDao.checkTopClick(start, end, code, mainCabCode));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 5) {//盘验异常数 指定时间
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryUnusualNumClick(start, end, code, mainCabCode));
        }
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime) && click == 6) {//盘验缺失数
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            return JsonResult.success("查询成功", cabinetFormDao.checkTopQueryDefectNumClick(start, end, code, mainCabCode));
        }
        return JsonResult.fail("查询失败");
    }

    @Override
    public JsonResult midFirstChart(String type, String startTime, String endTime, String mainCabCode, String code, String param, String other) {
        HashMap hm1 = new HashMap();
        if (other.equals("person")) {
            hm1.put("title", "盘验存入记录(按人员)");
        } else if (other.equals("device")) {
            hm1.put("title", "盘验存入记录(按设备)");
        } else {
            hm1.put("title", "盘验存入记录(按物料)");
        }
        List<HashMap> list1 = new ArrayList<>();
        List list = new ArrayList();
        HashMap map = new HashMap();
        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
            list1.addAll(checkReportDao.queryInRecords(start, end, param, code, mainCabCode, other));
            if(other.equals("person")){
                map.putAll(checkReportDao.queryInRecordsTotal(start,end,code,mainCabCode,param));
            }
        }
        if (type.equals("0")) {
            list1.addAll(checkReportDao.queryInRecords(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), param, code, mainCabCode, other));
            if(other.equals("person")){
                map.putAll(checkReportDao.queryInRecordsTotal(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1),code,mainCabCode,param));
            }
        } else if (type.equals("1")) {
            list1.addAll(checkReportDao.queryInRecords(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), param, code, mainCabCode, other));
            if(other.equals("person")){
                map.putAll(checkReportDao.queryInRecordsTotal(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1),code,mainCabCode,param));
            }
        } else if (type.equals("2")) {
            list1.addAll(checkReportDao.queryInRecords(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), param, code, mainCabCode, other));
            if(other.equals("person")){
                map.putAll(checkReportDao.queryInRecordsTotal(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1),code,mainCabCode,param));
            }
        }
        if(other.equals("person")){
            list.add(map);
            hm1.put("totalSumOne",list);
        }
        hm1.put("data", list1);
        return JsonResult.success("查询成功", hm1);
    }

    @Override
    public JsonResult midSecondChart(String type, String startTime, String endTime, String mainCabCode, String code, String param, String other, String option) {
        HashMap hm1 = new HashMap();
        if (option.equals("rep")) {
            if (other.equals("person")) {
                hm1.put("title", "谁报修最多");
            } else if (other.equals("skuCode")) {
                hm1.put("title", "哪款产品报修记录最多");
            }else if(other.equals("order")){
                hm1.put("title","哪个工单报修记录最多");
            }else if(other.equals("device")){
                hm1.put("title","来自哪台设备报修记录最多");
            }
        } else if (option.equals("src")) {
            if (other.equals("person")){
                hm1.put("title", "谁报废最多");
            } else if (other.equals("skuCode")) {
                hm1.put("title", "哪款产品报废记录最多");
            }else if(other.equals("order")){
                hm1.put("title","哪个工单报废记录最多");
            }else if(other.equals("device")){
                hm1.put("title","来自哪台设备报废记录最多");
            }
        } else if (option.equals("loss")) {
            if (other.equals("person") ) {
                hm1.put("title", "谁报失最多");
            } else if (other.equals("skuCode")) {
                hm1.put("title", "哪款产品报失记录最多");
            }else if(other.equals("order")){
                hm1.put("title","哪个工单报失记录最多");
            }else if(other.equals("device")){
                hm1.put("title","来自哪台设备报失记录最多");
            }
        }
            List<HashMap> list1 = new ArrayList<>();
            List list = new ArrayList();
            HashMap map = new HashMap();
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime, "yyyy-MM-dd HH:mm:ss");
                LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime, "yyyy-MM-dd HH:mm:ss");
                list1.addAll(checkReportDao.queryCheckedRecords(start, end, param, code, mainCabCode, other, option));
                if(other.equals("person")){
                    map.putAll(checkReportDao.queryCheckedRecordsTotal(start, end,code,mainCabCode,param,option));
                }
            }
            if (type.equals("0")) {
                list1.addAll(checkReportDao.queryCheckedRecords(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1), param, code, mainCabCode, other, option));
                if(other.equals("person")){
                    map.putAll(checkReportDao.queryCheckedRecordsTotal(ReportUtil.dayUtil().get(0), ReportUtil.dayUtil().get(1),code,mainCabCode,param,option));
                }
            } else if (type.equals("1")) {
                list1.addAll(checkReportDao.queryCheckedRecords(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1), param, code, mainCabCode, other, option));
                if(other.equals("person")){
                    map.putAll(checkReportDao.queryCheckedRecordsTotal(ReportUtil.weekUtil().get(0), ReportUtil.weekUtil().get(1),code,mainCabCode,param,option));
                }
            } else if (type.equals("2")) {
                list1.addAll(checkReportDao.queryCheckedRecords(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1), param, code, mainCabCode, other, option));
                if(other.equals("person")){
                    map.putAll(checkReportDao.queryCheckedRecordsTotal(ReportUtil.dateUtil().get(0), ReportUtil.dateUtil().get(1),code,mainCabCode,param,option));
                }
            }
            if(other.equals("person")){
                list.add(map);
                hm1.put("totalSumOne",list);
            }
            hm1.put("data", list1);
            return JsonResult.success("查询成功", hm1);
        }



    @Override
    public JsonResult midThirdChart(String type, String startTime, String endTime, String mainCabCode, String param, String code) {
        HashMap hm1 = new HashMap();
        hm1.put("title","物料盘验");
        List<HashMap> list1 = new ArrayList<>();
        if (StringUtils.isNotBlank(startTime)&&StringUtils.isNotBlank(endTime)){
            LocalDateTime start = LocalDateTimeUtils.convertStringToLDT(startTime,"yyyy-MM-dd HH:mm:ss");
            LocalDateTime end = LocalDateTimeUtils.convertStringToLDT(endTime,"yyyy-MM-dd HH:mm:ss");
            list1.addAll(checkReportDao.midThirdChart(start,end,param,code,mainCabCode));
        }
        if (type.equals("0")){
            list1.addAll(checkReportDao.midThirdChartByDay(ReportUtil.dayUtil().get(0),ReportUtil.dayUtil().get(1),param,code, mainCabCode));
        }else if (type.equals("1")){
            list1.addAll(checkReportDao.midThirdChart(ReportUtil.weekUtil().get(0),ReportUtil.weekUtil().get(1),param,code, mainCabCode));
        }else if (type.equals("2")){
            list1.addAll(checkReportDao.midThirdChart(ReportUtil.dateUtil().get(0),ReportUtil.dateUtil().get(1),param,code, mainCabCode));
        }
        hm1.put("data",list1);
        return JsonResult.success("查询成功",hm1);
    }
}
